require 'parseexcel'
require 'iconv'
class AddDataUsers < ActiveRecord::Migration
  def self.up
        data_file_path = File.join(File.dirname(__FILE__), "/data", "import_data.xls")
        workbook = Spreadsheet::ParseExcel.parse(data_file_path)
        worksheet = workbook.worksheet(0)
        worksheet.each(1) do |row|
            unless row.nil?
                user = User.new
                user.id = row.at(0) unless row.at(0).nil?
                user.name = row.at(1).to_s('latin1') unless row.at(1).nil?
                user.hashed_password = row.at(2).to_s('latin1') unless row.at(2).nil?
                user.nickname = Iconv.conv('UTF-8', 'GBK', row.at(3).to_s('GBK')) unless row.at(3).nil?
                user.email = row.at(4).to_s('latin1') unless row.at(4).nil?
                user.salt = row.at(5).to_s('latin1') unless row.at(5).nil?
                user.created_at = row.at(6).date unless row.at(6).nil?
                user.updated_at = row.at(7).date unless row.at(7).nil?
                user.save!
            end
  end

  def self.down
    users.delete_all
  end
  end
end
